<template>
  <div>
    <el-tabs v-model="editableTabsValue"  @tab-remove="removeTab" @tab-click="clickTab">
      <el-tab-pane :name="defaultActiveName" label="员工账号列表">
        <user-search-bar v-on:search="search"></user-search-bar>
        <el-button-group style="margin-bottom: 8px;float: left;">
          <el-button size="mini" type="primary" @click="create" v-has-permission="'UserList.joined'" icon="el-icon-plus">新建</el-button>
          <el-button size="mini" type="primary" @click="update" v-has-permission="'UserList.update'" icon="el-icon-edit">修改</el-button>
          <el-button size="mini" type="primary" @click="deleteById" v-has-permission="'UserList.delete'" icon="el-icon-delete">删除</el-button>
          <el-button size="mini" type="primary" @click="importByExcel" v-has-permission="'UserList.import'" icon="el-icon-upload2">导入</el-button>
          <el-button size="mini" type="primary" v-export-excel-directive="exportExcelPar" v-has-permission="'UserList.export'" icon="el-icon-download">导出</el-button>
        </el-button-group>
        <el-table
          border
          v-loading="loading"
          size="mini"
          :data="baseDto.lstDto"
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            prop="loginName"
            label="登录账号"
            width="150">
          </el-table-column>
          <el-table-column
            prop="realName"
            label="姓名"
            width="220">
          </el-table-column>
          <el-table-column
            prop="mobilePhone"
            label="联系电话"
            width="100">
          </el-table-column>
          <el-table-column
            prop="typeLabel"
            label="用户类型"
            width="120">
          </el-table-column>
          <el-table-column
            prop="sexLabel"
            label="性别"
            width="80">
          </el-table-column>
          <el-table-column
            prop="email"
            label="邮箱"
            width="150">
          </el-table-column>
          <el-table-column
            prop="statusLabel"
            label="状态"
            width="80">
          </el-table-column>
          <el-table-column
            prop="branchCompanyName"
            label="所属公司"
            width="200">
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="pageSizeChange"
          @current-change="pageIndexChange"
          :current-page="searchModel.pageDto.pageNum"
          :page-sizes="searchModel.pageDto.pageSizes"
          :page-size="searchModel.pageDto.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="baseDto.page.total">
        </el-pagination>
      </el-tab-pane>
      <el-tab-pane
        v-for="(item) in editableTabs"
        :key="item.name"
        :label="item.label"
        :name="item.name"
        closable>
        <user-invite v-if="item.key == 'invite'"></user-invite>
        <user-create v-if="item.key == 'create'"></user-create>
        <user-update v-if="item.key == 'update'" v-bind:selectedModel="selectedModel"></user-update>
        <user-import v-if="item.key == 'import'"></user-import>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import Vue from 'vue'
import UserCreate from './user-create'
import UserUpdate from './user-update'
import UserImport from './user-import'
import UserSearchBar from './user-search-bar'
import UserInvite from './user-invite'

export default {
  name: 'user-list',
  components: {
    UserInvite,
    UserSearchBar,
    UserImport,
    UserCreate,
    UserUpdate
  },
  data () {
    return {
      loading: true,
      searchModel: {
        pageDto: {pageSize: 10, pageNum: 1, pageSizes: [10, 20, 30, 100]},
        loginName: null,
        realName: null,
        sex: null,
        email: null,
        type: null,
        status: null
      },
      baseDto: {page: {total: 0}},
      selectedModel: {},
      exportExcelPar: {
        fileType: 'xls',
        xmlName: 'admin_user',
        moduleName: 'user',
        server: Vue.$adminServerURL
      },
      defaultActiveName: 'UserList',
      editableTabsValue: 'UserList',
      editableTabs: [],
      tabIndex: 2,
      checkedDataList: [] // 选择的数据集合
    }
  },
  created: function () {
    this.listPage()
  },
  methods: {
    handleSelectionChange: function (val) {
      this.checkedDataList = val
    },
    listPage: function () {
      this.loading = true
      Vue.$ajax({
        method: 'post',
        url: Vue.$adminServerURL + '/web/v1/UserController/listPage',
        data: this.searchModel
      }).then(res => {
        this.loading = false
        if (res.data.flag === 'SUCCESS') {
          this.baseDto = res.data
        } else {
          if (!Vue.$isNullOrIsBlankOrIsUndefined(res.data.message)) {
            this.$message({
              message: res.data.message,
              type: 'warning'
            })
          }
        }
      })
    },
    pageSizeChange: function (e) {
      this.searchModel.pageDto.pageSize = e
      this.listPage()
    },
    pageIndexChange: function (e) {
      this.searchModel.pageDto.pageNum = e
      this.listPage()
    },
    search: function (p) {
      if (!p.isCollapse) { // 非高级查询，则只需要根据关键字查询
        this.searchModel.loginName = p.searchKey
      } else {
        this.searchModel = p.searchModel
      }
      this.searchModel.pageDto.pageNum = 1
      this.listPage()
    },
    invite: function () {
      this.addTab('邀请入职', 'InviteUser', 'invite')
    },
    create: function () {
      this.addTab('新建', 'CreateUser', 'create')
    },
    update: function () {
      if (this.checkedDataList == null || this.checkedDataList.length !== 1) {
        this.$message({
          message: '请选择一条数据',
          type: 'warning'
        })
        return
      }

      let data = this.checkedDataList[0]

      this.selectedModel = data
      this.addTab('编辑-' + data.loginName, data.id + '', 'update')
    },
    deleteById: function () {
      if (this.checkedDataList == null || this.checkedDataList.length !== 1) {
        this.$message({
          message: '请选择一条数据',
          type: 'warning'
        })
        return
      }

      let data = this.checkedDataList[0]

      this.$confirm('您确定删除该记录吗？', '询问', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Vue.$ajax({
          method: 'delete',
          url: Vue.$adminServerURL + '/web/v1/UserController/' + data.id
        }).then(res => {
          if (res.data.flag !== 'SUCCESS') {
            this.$message({
              message: res.data.message,
              type: 'warning'
            })
          } else {
            this.$message({
              message: res.data.message,
              type: 'success'
            })
            this.listPage()
          }
        })
      }).catch(() => {
      })
    },
    importByExcel: function () {
      this.addTab('导入用户', 'ImportUser', 'import')
    },
    addTab: function (label, name, key) {
      let ele = {label: label, name: name, key: key}
      let flag = false

      for (let i = 0; i < this.editableTabs.length; i++) {
        let var1 = this.editableTabs[i]
        if (var1.name === name) {
          flag = true
          break
        }
      }

      if (!flag) {
        this.editableTabs.push(ele)
      }
      this.editableTabsValue = name
    },
    removeTab: function (targetName) {
      let tabs = this.editableTabs
      let activeName = this.editableTabsValue
      if (activeName === targetName) {
        tabs.forEach((tab, index) => {
          if (tab.name === targetName) {
            let nextTab = tabs[index + 1] || tabs[index - 1]
            if (nextTab) {
              activeName = nextTab.name
            } else {
              activeName = this.defaultActiveName
              this.listPage()
            }
          }
        })
      }

      this.editableTabsValue = activeName
      this.editableTabs = tabs.filter(tab => tab.name !== targetName)
    },
    clickTab: function (tab) {
      if (tab.name === this.defaultActiveName) {
        this.listPage()
      }
    }
  }
}
</script>
